<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <template v-slot:label>
              <span id="title">角色管理</span>
            </template>
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column
                sortable
                label="角色名称"
                property="name"
                width="240"
              />
              <el-table-column sortable label="描述" property="description" />
              <el-table-column label="操作">
                <template slot-scope="{ row }">
                  <el-button
                    size="small"
                    type="text"
                    @click="showPerDialog(row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="text"
                    @click="editRole(row.id)"
                  >编辑</el-button>
                  <el-popconfirm
                    title="这是一段内容确定删除吗？"
                    @onConfirm="delRole(row.id)"
                  >
                    <el-button
                      slot="reference"
                      size="small"
                      type="text"
                    >删除</el-button>
                  </el-popconfirm>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <!-- <span class="pageSize">共 {{ page.total }} 条</span> -->
              <el-pagination
                :current-page.sync="page.page"
                :page-size.sync="page.pagesize"
                layout="total,prev, pager, next"
                :total="page.total"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <template v-slot:label>
              <span id="title">公司信息</span>
            </template>
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <company-set />
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 编辑弹窗 -->
      <el-dialog
        title="编辑角色"
        :visible.sync="dialogFormVisible"
        @close="closeRole"
      >
        <el-form ref="shure" :model="form" :rules="rules">
          <el-form-item
            prop="name"
            label="角色名称"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.name" autocomplete="off" />
          </el-form-item>
          <el-form-item
            prop="description"
            label="角色描述"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.description" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="btnOK">确 定</el-button>
        </div>
      </el-dialog>
      <perDialog ref="perDialog" />
    </div>
  </div>
</template>
<script>
// 导入弹窗
import perDialog from './components/perDialog.vue'
import companySet from './components/companySet.vue'
import {
  addRole,
  deleteRole,
  getRoleDetail,
  getRoleList,
  updateRole
} from '../../api/setting'
export default {
  components: {
    companySet,
    perDialog
  },
  data() {
    return {
      currentPage1: 1,
      // 是否显示弹窗
      dialogFormVisible: false,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名不能为空', trigger: 'blur' }],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      },
      formLabelWidth: '120px',
      list: [],
      page: {
        page: 1,
        pagesize: 4,
        total: 0 // 记录总数
      }
    }
  },

  created() {
    this.getRoleInfo()
  },
  methods: {
    // 权限弹窗
    showPerDialog(id) {
      this.$refs.perDialog.show(id)
      // console.log(id)
    },
    // 关闭弹窗
    closeRole() {
      if (this.form.id) {
        this.form = {
          name: '',
          description: ''
        }
      }
    },
    // 新增角色
    addRole() {
      this.dialogFormVisible = true
    },

    // 获取角色数据
    async getRoleInfo() {
      // 获取角色列表
      const res = await getRoleList(this.page)
      // console.log(res)
      this.page.total = res.total
      this.list = res.rows
    },
    // 编辑
    async editRole(id) {
      const res = await getRoleDetail(id)
      // console.log(res)
      this.form = res
      this.dialogFormVisible = true
    },
    // 点击确定
    /* eslint-disable */
    btnOK() {
      this.$refs.shure.validate(async valid => {
        if (valid) {
          // 校验通过
          // alert('submit!')
          // 调用修改角色接口
          if (this.form.id) {
            const res = await updateRole(this.form)
            this.$message.success('修改成功')
          } else {
            const res = await addRole(this.form)
            this.$message.success('新增成功')
          }
          // console.log(this.form.id)
          // 提示用户修改成功
          // 关闭弹窗
          this.dialogFormVisible = false
          // 更新数据
          this.getRoleInfo()
          this.form = {
            name: '',
            description: ''
          }
        } else {
          this.$message.error('输入错误，请重新输入')
          return false
        }
      })
    },
    // 删除
    async delRole(id) {
      await deleteRole(id)
      this.$message.success('删除成功')
      if (this.list.length === 1 && this.page.page > 1) {
        this.page.page--
      }
      this.getRoleInfo()
    },
    // 页码点击
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.getRoleInfo()
    }
    // changePage(newPage) {
    //   this.getRoleList()
    //   this.page.page = newPage
    // }
  }
}
</script>
<style lang="scss">
.pageSize {
  font-size: 14px;
}
.el-tabs__item {
  font-size: 20px;
  #title {
    padding: 0 20px;
  }
}
</style>
